package com.dudu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.mortbay.log.Log;

import com.dudu.bean.IfControlBean;
import com.dudu.bean.IfControlCheckConclusionPushBean;
import com.dudu.bean.IfControlRuleBean;
import com.dudu.dao.InfoControlDao;
import com.dudu.dao.InfoControlRuleDao;
import com.dudu.service.IInfoControl;
import com.dudu.service.support.IInfoControlProcessor;
import com.dudu.service.support.InfoControlProcessorCreator;
import com.dudu.utils.DateUtils;
import com.dudu.utils.DictionaryUtils;

import net.sf.json.JSONObject;

/**
 * 信控处理服务类
 * 
 * @author zx
 *
 */
public class InfoControlServiceImpl implements IInfoControl {

	/*
	 * 线程池，线程数量固定为100
	 */
	private static ExecutorService executor = Executors.newFixedThreadPool(100);

	/**
	 * 信控入口，发送语音通知或文字留言后调用
	 */
	@Override
	public void infoControl(IfControlBean ifBean, Map<String, String> parameterMap) {
		Calendar cal = Calendar.getInstance();
		int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (dayOfWeek >= 2 && dayOfWeek <= 6) {
			/**
			 * 只有工作日才进行信控处理,周一至周五
			 * 此处有问题，因为周一至周五可能是休息日
			 */
			InfoControlRuleDao ifrDao = new InfoControlRuleDao();
			String hm = DateUtils.getTimeStringNow().substring(8, 12);
			IfControlRuleBean ifrBean = new IfControlRuleBean();
			ifrBean.setAbilityType(ifBean.getAbilityType());
			ifrBean.setAppKey(ifBean.getAppKey());
			ifrBean.setUseFlag("yes");
			ifrBean.setStartTime(hm);
			ifrBean.setEndTime(hm);
			List<JSONObject> rules = ifrDao.queryIfControlRules(ifrBean, true);
			Log.info("查询到信控监控规则" + rules);
			/**
			 * 查询到匹配的规则后才调用信控处理服务
			 */
			if (rules != null && !rules.isEmpty()) {
				List<IfControlRuleBean> ifcRules = new ArrayList();
				for (int i = 0; i < rules.size(); i++) {
					JSONObject jb = rules.get(i);
					IfControlRuleBean rule = new IfControlRuleBean();
					rule.setCheckNumbers(jb.getString("check_numbers"));
					rule.setCheckPeriod(jb.getInt("check_period"));
					rule.setDisplaynumRecord(jb.getString("displaynum_record"));
					rule.setMaxSendCount(jb.getInt("send_count"));
					rule.setId(jb.getInt("id"));
					ifcRules.add(rule);
				}
				/**
				 * 多线程处理
				 */
				ifBean.setStatus("0");// 审核中
				executor.execute(new InfoControlThread(ifBean, parameterMap, ifcRules, true));
			}
		}
	}

	/**
	 * 短信反馈语音通知审核结果的后续处理
	 */
	@Override
	public void infoControlSMSFeedback(IfControlBean ifBean) {
		executor.execute(new InfoControlThread(ifBean, null, null, false));
	}

	/**
	 * 查询信控规则
	 */
	@Override
	public List<JSONObject> queryAllInfoControlRules(String pageSql) {
		InfoControlRuleDao ruleDao = new InfoControlRuleDao();
		return ruleDao.queryALLIfControlRules(pageSql);
	}

	/**
	 * 查询信控规则数量
	 */
	@Override
	public int queryALLIfControlRulesCount() {
		InfoControlRuleDao ruleDao = new InfoControlRuleDao();
		return ruleDao.queryALLIfControlRulesCount();
	}

	/**
	 * 创建信控规则
	 */
	@Override
	public int createIfControlRule(IfControlRuleBean ruleBean) {
		InfoControlRuleDao ruleDao = new InfoControlRuleDao();
		return ruleDao.createControlRule(ruleBean);
	}

	/**
	 * 查询信控审核人
	 */
	@Override
	public String queryCheckers() {
		String checkers = DictionaryUtils.getValueByTypeAndKey("infoControl", "checkers");
		return checkers;
	}

	/**
	 * 更新信控规则
	 */
	@Override
	public int updateIfControlRule(IfControlRuleBean ruleBean) {
		InfoControlRuleDao ruleDao = new InfoControlRuleDao();
		return ruleDao.updateControlRule(ruleBean);
	}

	/**
	 * 启用/停用信控规则
	 */
	@Override
	public int updateIfControlRuleStatus(IfControlRuleBean ruleBean) {
		InfoControlRuleDao ruleDao = new InfoControlRuleDao();
		return ruleDao.updateControlRuleStatus(ruleBean);
	}

	/**
	 * 信控监控超时转历史
	 */
	@Override
	public boolean finishInfoControlOverTime(IfControlBean ifBean) {
		InfoControlDao icDao = new InfoControlDao();
		ifBean.setStatus("2");// 审核超时
		return icDao.finishIfControlProcess(ifBean);
	}

	/**
	 * 增加信控审核结果推送记录
	 */
	@Override
	public int addIfControlCheckConclusionPushInfo(IfControlCheckConclusionPushBean bean) {
		InfoControlDao icDao = new InfoControlDao();
		return icDao.addIfControlCheckConclusionPushInfo(bean);
	}

	/**
	 * 查询信控推送url
	 */
	@Override
	public String queryIfControlPushURL(String appKey, String custAccount) {
		InfoControlDao icDao = new InfoControlDao();
		return icDao.queryIfControlUrlByAccountInfo(custAccount, appKey);
	}

	/**
	 * 根据主键查询信控监控
	 */
	@Override
	public IfControlBean queryIfControlById(int id) {
		InfoControlDao icDao = new InfoControlDao();
		IfControlBean ifControl = new IfControlBean();
		ifControl.setId(id);
		return icDao.queryIfControlById(ifControl);
	}

	/**
	 * 信控线程类，内部使用
	 * 
	 * @author zx
	 *
	 */
	private static class InfoControlThread implements Runnable {

		private IfControlBean ifBean;

		private Map<String, String> parameterMap;

		private List<IfControlRuleBean> ifcRules;

		private boolean startProcess;

		InfoControlThread(IfControlBean ifBean, Map<String, String> parameterMap, List<IfControlRuleBean> ifcRules,
				boolean startProcess) {
			this.ifBean = ifBean;
			this.parameterMap = parameterMap;
			this.ifcRules = ifcRules;
			this.startProcess = startProcess;
		}

		@Override
		public void run() {
			IInfoControlProcessor processor = InfoControlProcessorCreator.getProcessor(ifBean, ifcRules, parameterMap);
			if(processor!=null){
				if (startProcess)
					processor.process();
				else
					processor.postProcess();
			}
		}

	}

}
